package com.ledi.guns.core.util.baiduUtil.define;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.ledi.guns.core.util.baiduUtil.Encoder;

public class BaseState implements State {

	private boolean state = false;
	private String info = null;

	private Map<String, String> infoMap = new HashMap<String, String>();

	public BaseState() {

		state = true;
	}

	public BaseState(boolean state) {

		setState(state);
	}

	public BaseState(boolean state, String info) {

		setState(state);
		this.info = info;
	}

	public BaseState(boolean state, int infoCode) {

		setState(state);
		info = AppInfo.getStateInfo(infoCode);
	}

	@Override
	public boolean isSuccess() {

		return state;
	}

	public void setState(boolean state) {

		this.state = state;
	}

	public void setInfo(String info) {

		this.info = info;
	}

	public void setInfo(int infoCode) {

		info = AppInfo.getStateInfo(infoCode);
	}

	@Override
	public String toJSONString() {

		return toString();
	}

	@Override
	public String toString() {

		String key = null;
		String stateVal = isSuccess() ? AppInfo.getStateInfo(AppInfo.SUCCESS) : info;

		StringBuilder builder = new StringBuilder();

		builder.append("{\"state\": \"" + stateVal + "\"");

		Iterator<String> iterator = infoMap.keySet().iterator();

		while (iterator.hasNext()) {

			key = iterator.next();

			builder.append(",\"" + key + "\": \"" + infoMap.get(key) + "\"");

		}

		builder.append("}");

		return Encoder.toUnicode(builder.toString());

	}

	@Override
	public void putInfo(String name, String val) {

		infoMap.put(name, val);
	}

	@Override
	public void putInfo(String name, long val) {

		this.putInfo(name, val + "");
	}

}
